package com.pluto.question.controller;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.pluto.api.controller.dto.MyResp;
import com.pluto.question.entity.Answer;
import com.pluto.question.entity.Worker;
import com.pluto.question.repo.AnswerRepo;
import com.pluto.question.repo.WorkerRepo;
import com.pluto.question.util.CodeUtil;

@RestController
public class IndexController {

	@Autowired
	private WorkerRepo workerRepo;
	@Autowired
	private AnswerRepo answerRepo;
	@GetMapping("/workers")
	public MyResp workers() {
		String str=" @@  @@  @@  @@  @@  @@  @@  @@ ";
		System.out.println(str.split("@@").length);
		return MyResp.create().addItems(workerRepo.findAll()); 
	}
	
	@PostMapping("/answer")
	public MyResp answer(HttpServletRequest request,@RequestBody Answer answer) {
		answer.setCode(CodeUtil.geneCode("a"));
		answer.setIp(request.getRemoteAddr());
		answer.setCreatetime(Timestamp.from(Instant.now()));
		Answer retAnswer=answerRepo.save(answer);
		return MyResp.create().addItemMap("answer", retAnswer);
	}
	
	@GetMapping("/answers")
	public MyResp answers() {
		return MyResp.create().addItems(answerRepo.findAll());
	}
	
	@PostMapping("/worker")
	public MyResp addWorker(@RequestBody Worker worker) {
		worker.setCode(CodeUtil.geneCode("u"));
		Worker retWorker=workerRepo.save(worker);
		return MyResp.create().addItemMap("worker", retWorker);
	}
	
	@DeleteMapping("/worker")
	public MyResp delWorker(@RequestBody Worker worker){
		workerRepo.delete(worker);
		return MyResp.create();
	}
}
